﻿using Direct.IDAL.User.Jurisdiction;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Jurisdiction
{
    /// <summary>
    /// 会员升级数据访问层
    /// </summary>
    public class UserUpgradeRepository:AbsRepository, IUserUpgradeRepository
    {
        /// <summary>
        /// 插入会员升级表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hsTable"></param>
        public void UpApply(UserUpGradeReq model, ref Hashtable hsTable)
        {
            var str = new StringBuilder();
            str.Append(" INSERT INTO UserUpgrade ( ");
            str.Append(" [NowLevel], ");
            str.Append(" [Amount], ");
            str.Append(" [UpgradeDate], ");
            str.Append(" [UpgradeState], ");
            str.Append(" [AfterLevel], ");
            str.Append(" [UserCode] )");
            str.Append(" VALUES ");
            str.Append(" (@NowLevel, @Amount, @UpgradeDate, @UpgradeState,@AfterLevel,@UserCode); ");
            hsTable.Add(str.ToString(), model);
        }
        /// <summary>
        /// 计算差额
        /// </summary>
        /// <param name="model"></param>
        public async Task<decimal> CalcAmount(UserUpGradeReq model)
        {
            var str = new StringBuilder();
            str.AppendFormat(" select  (select  Amount from MemberLevel where LevelID = '{0}'  ) -  ",model.AfterLevel);
            str.AppendFormat(" (select Amount from MemberLevel where LevelID = '{0}'  )   as 'Amount' ",model.NowLevel);
            return await Task.Run(()=> {
              return  base.QueryFirstT<decimal>(str.ToString());
                });
        }
        /// <summary>
        /// 判断是否重复提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> IsRepeat(UserUpGradeReq model)
        {
            var str = new StringBuilder();
            str.Append(" SELECT COUNT(*) FROM UserUpgrade ");
            str.AppendFormat(" WHERE UpgradeState=0 AND UserCode='{0}' ", model.UserCode);
            return await Task.Run(() => base.QueryFirstT<int>(str.ToString()) <= 0);
        }
        /// <summary>
        /// 获取可以晋升的级别
        /// </summary>
        /// <returns></returns>
        public IList<MemberLevel> QueryMemberLevel(byte level)
        {
            StringBuilder str = new StringBuilder();
            str.Append(" SELECT ");
            str.Append(" LevelName, ");
            str.Append(" Amount, ");
            str.Append(" LevelID, ");
            str.Append(" LevelName +'|'+CAST ( Amount AS VARCHAR ) as SelectFileds ");
            str.Append(" FROM ");
            str.AppendFormat(" MemberLevel WHERE LevelID > {0} ",level);
            return base.Query<MemberLevel>(str.ToString()) as IList<MemberLevel>;
        }
        /// <summary>
        /// 查询会员升级记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<PagedList<UserUpgrade>> Upgraderecord(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id ASC",
                PageSize = pagesize,
                PageIndex = pageindex,
                StrWhere = $"UserCode='{usercode}'",
                TableName = "UserUpgrade"
            };
            return await base.PageQuery<UserUpgrade>(model);
            //StringBuilder str = new StringBuilder().Append(" SELECT * FROM UserUpgrade ")
            //    .AppendFormat(" WHERE UserCode='{0}' ", usercode);
            //return await base.QueryAsync<UserUpgrade>(str.ToString()) as IList<UserUpgrade>;
        }
    }
}
